from django.conf import settings
import redis

redis_client_gather = {}


class RedisHelper:
    """
    redis 客户端
    """

    def __init__(self, config="default"):
        self.redis_client = self.redis_connect(config)

    @staticmethod
    def redis_connect(config):
        if not redis_client_gather.get(config):
            connect_config = settings.REDIS.get(config)
            connect_config["decode_responses"] = True
            redis_pool = redis.ConnectionPool(**connect_config)
            redis_client = redis.Redis(connection_pool=redis_pool)
            redis_client_gather["config"] = redis_client
            return redis_client
        else:
            return redis_client_gather.get(config)
